import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      component: () => import("../components/login.vue"),
    },
    {
      path: "/home",
      redirect: "/welcome",
      component: () => import("../components/home.vue"),
      children: [
        {
          path: "/welcome",
          component: () => import("../components/welcome.vue"),
        },
        {
          path: "/users",
          component: () => import("../components/user/users.vue"),
        },
        {
          path: "/rights",
          component: () => import("../components/power/rights.vue"),
        },
        {
          path: "/roles",
          component: () => import("../components/power/roles.vue"),
        },
        {
          path: "/categories",
          component: () => import("../components/goods/cate.vue"),
        },
        {
          path: "/params",
          component: () => import("../components/goods/params.vue"),
        },
        {
          path: "/goods",
          component: () => import("../components/goods/list.vue"),
        },
        {
          path: "/goods/add",
          component: () => import("../components/goods/add.vue"),
        },
        {
          path: "/orders",
          component: () => import("../components/order/orders.vue"),
        },
        {
          path: "/reports",
          component: () => import("../components/reports/reports.vue"),
        },
      ],
    },
  ],
});
router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  const tokenStr = window.sessionStorage.getItem("login");
  if (!tokenStr) return next("/login");
  next();
});
export default router;
